package com.xxx.websocket;

import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import static io.netty.handler.codec.http.HttpMethod.POST;

/**
 * loginInboundHandler
 *
 * @author zk
 * @since 2018/9/13
 */
public class LoginInboudHandler extends SimpleChannelInboundHandler<DefaultFullHttpRequest> {
    public static List<String> userNames = new ArrayList<>();
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DefaultFullHttpRequest msg) throws Exception {
        String url = msg.uri();
        if(url.equals("/register") || POST == msg.method()){
            String username = msg.content().toString(Charset.forName("UTF-8"));
            userNames.add(username);
            HttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK, Unpooled.wrappedBuffer("{\"flag\":0}".getBytes()));
            httpResponse.headers().add("content-type","application/json");
            httpResponse.headers().add("transfer-encoding","trunked");
            ctx.channel().writeAndFlush(httpResponse);
        } else {
            HttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK, Unpooled.wrappedBuffer("你请求的是啥".getBytes()));
            httpResponse.headers().add("content-type","application/json");
            httpResponse.headers().add("transfer-encoding","trunked");
        }
    }
}
